package Actions;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class FileUploadAction extends Action {
	
	private FormFile myFile = null;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		forms.FileUploadForm f = (forms.FileUploadForm) form;
		
		request.getSession().setAttribute("discp", f.getDiscp());
		request.getSession().setAttribute("Dmonth", f.getDmonth());
		request.getSession().setAttribute("Dyear", f.getDyear());
		request.getSession().setAttribute("GPA", f.getGpa());
		request.getSession().setAttribute("title", f.getTitle());
		
		// Process the FormFile
		myFile = f.getFile();

		String fileName = myFile.getFileName();
		int fileSize = myFile.getFileSize();

		request.getSession().setAttribute("filename", fileName);
		request.getSession().setAttribute("filesize", fileSize);
		
		f.setFileName(fileName);
		f.setFileSize(fileSize);
		/*
		 * // Process the FormFile
			FormFile myFile = myForm.getMyFile();
			String contentType = myFile.getContentType();
			String fileName = myFile.getFileName();
			int fileSize = myFile.getFileSize();
			byte[] fileData = myFile.getFileData();
			
			
			Connection conn = DbConnectionPool.getConnection();
			String selectStr = "insert into file_uploads ( fname, file, fsize, contentType ) values (?,?,?,?);";
			PreparedStatement pstmt = conn.prepareStatement(selectStr);
			pstmt.setString(1, fileName);
			pstmt.setBinaryStream(2, myFile.getInputStream(), myFile.getFileSize());
			pstmt.setInt(3, fileSize);
			pstmt.setString(4, contentType);
			pstmt.execute();
			conn.commit();
		 */
		try {
			f.setMyFile(myFile.getInputStream());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    	return mapping.findForward("degreesuccess");
	}
}
